<!DOCTYPE html>
<html lang="zh-CN">

	<head>
		<meta charset="UTF-8">
		<link rel='dns-prefetch' href='//s.w.org' />
		<link rel='stylesheet' id='pure-highlightjs-style-css' href='http://www.codeorigin.cn/wp-content/plugins/Pure-Highlightjs_1.0/highlight/styles/default.css?ver=0.9.2' type='text/css' media='all' />
		<link rel='stylesheet' id='pure-highlightjs-css-css' href='http://www.codeorigin.cn/wp-content/plugins/Pure-Highlightjs_1.0/assets/pure-highlight.css?ver=0.1.0' type='text/css' media='all' />
		<link rel='stylesheet' id='wp-syntax-css-css' href='http://www.codeorigin.cn/wp-content/plugins/wp-syntax/css/wp-syntax.css?ver=1.0' type='text/css' media='all' />
		<link rel='stylesheet' id='style-css' href='http://www.codeorigin.cn/wp-content/themes/wp-dfrobot-po/style.css?ver=2016.03.09' type='text/css' media='screen' />
		<link rel='stylesheet' id='font-awesome-css' href='http://www.codeorigin.cn/wp-content/themes/wp-dfrobot-po/css/font-awesome.min.css?ver=1.0' type='text/css' media='all' />
		<link rel='stylesheet' id='highlightx-css' href='http://www.codeorigin.cn/wp-content/themes/wp-dfrobot-po/css/highlightx.css?ver=1.0' type='text/css' media='all' />
		<link rel="shortcut icon" href="http://www.codeorigin.cn/wp-content/myuploads/2016/10/favicon.jpg" type="image/x-icon" />
		<style type="text/css">
			.recentcomments a {
				display: inline !important;
				padding: 0 !important;
				margin: 0 !important;
			}
		</style>
		<link rel="shortcut icon" href="http://www.codeorigin.cn/wp-content/myuploads/2016/10/favicon.jpg">
		<link rel="stylesheet" href="nav.css" />
		<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
		<script src="ArticleMenu.js"></script>
		<title>如何书写正则表达式(RegPxp) | IT技术文章分享|Jquery|前端|nodejs|Linux</title>
	</head>
	<!--头部横条开始 mark-->

	<body>

		<div class="toptb cl">
			<div class="wp">
				<span class="left">欢迎您光临本站，如有任何问题可在此站留言，我将在第一时间回复您。</span>
			</div>
		</div>
		<!--nav代码开始-->
		<header class="deanhdtop screen">
			<div class="w1180">
				<hgroup class="deanlogo">
					<h1><a href="http://www.codeorigin.cn" title="IT技术文章分享|Jquery|前端|nodejs|Linux"><span>IT技术文章分享|Jquery|前端|nodejs|Linux</span></a></h1>
				</hgroup>
				<div class="ssq right">
					<div class="search right cl">
						<form method="get" id="searchform" action="/index.php">
							<input id="s" name="s" type="text" class="a swap_value text" placeholder="输入搜索关键词">
							<span class="btn-search">
				<i class="fa fa-search" onclick="$('#searchsubmit').click()"></i>
			</span>
							<input id="searchsubmit" type="submit" class="hidden" value="" />
						</form>
					</div>
					<div class="search_kz">
						<div class="boxtow">
							<span class="close2">×</span>
							<form action="http://www.codeorigin.cn" id="sm2">
								<input id="s" class="s swap_value" value="" placeholder="输入您要查找的关键词" name="s">
								<button type="submit" class="btn-search2" value=""> 
					<i class='fa fa-search'></i>
				</button>
							</form>
							<h2>热搜关键词：</h2>
							<div class="tag_bq">
								<a rel="nofollow" href='http://www.codeorigin.cn/tag/%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f' class='tag-link-28 tag-link-position-1' title='2个话题' style="color:#c832ea;font-size: 12pt;;">正则表达式</a>
								<a rel="nofollow" href='http://www.codeorigin.cn/tag/%e9%94%99%e8%af%af%e5%a4%84%e7%90%86' class='tag-link-25 tag-link-position-2' title='2个话题' style="color:#1e592b;font-size: 12pt;;">错误处理</a>
								<a rel="nofollow" href='http://www.codeorigin.cn/tag/%e6%8f%92%e4%bb%b6%e5%88%b6%e4%bd%9c' class='tag-link-19 tag-link-position-3' title='1个话题' style="color:#2ee55a;font-size: 12pt;;">插件制作</a>
								<a rel="nofollow" href='http://www.codeorigin.cn/tag/%e5%89%8d%e7%ab%af%e4%bc%98%e5%8c%96' class='tag-link-26 tag-link-position-4' title='2个话题' style="color:#bcc18;font-size: 12pt;;">前端优化</a>
								<a rel="nofollow" href='http://www.codeorigin.cn/tag/jquery' class='tag-link-18 tag-link-position-5' title='1个话题' style="color:#c608bf;font-size: 12pt;;">jquery</a>
								<a rel="nofollow" href='http://www.codeorigin.cn/tag/%e4%bb%a3%e7%a0%81%e6%b7%b7%e6%b7%86' class='tag-link-24 tag-link-position-6' title='1个话题' style="color:#31bb93;font-size: 12pt;;">代码混淆</a>
								<a rel="nofollow" href='http://www.codeorigin.cn/tag/%e4%bb%a3%e7%a0%81%e5%8a%a0%e5%af%86' class='tag-link-27 tag-link-position-7' title='1个话题' style="color:#1022ab;font-size: 12pt;;">代码加密</a>
								<a rel="nofollow" href='http://www.codeorigin.cn/tag/%e8%87%aa%e5%ae%9a%e4%b9%89%e6%8f%92%e4%bb%b6' class='tag-link-20 tag-link-position-8' title='1个话题' style="color:#5d26a5;font-size: 12pt;;">自定义插件</a>
							</div>
						</div>
					</div>
				</div>
				<nav class="deannav ">
					<ul id="menu-%e5%89%8d%e7%ab%af%e6%8a%80%e6%9c%af" class="header-menu-nav">
						<li id="menu-item-23" class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor current-menu-parent current-post-parent menu-item-has-children menu-item-23">
							<a href="http://www.codeorigin.cn/category/qdjs">前端技术</a>
							<ul class="sub-menu">
								<li id="menu-item-24" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-24">
									<a href="http://www.codeorigin.cn/category/qdjs/javascript">javascript</a>
								</li>
								<li id="menu-item-29" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-29">
									<a href="http://www.codeorigin.cn/category/qdjs/html5">html5</a>
								</li>
								<li id="menu-item-33" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-33">
									<a href="http://www.codeorigin.cn/category/qdjs/css3">css3</a>
								</li>
							</ul>
						</li>
						<li id="menu-item-25" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-25">
							<a href="http://www.codeorigin.cn/category/mongodb">MongoDb</a>
						</li>
						<li id="menu-item-28" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-28">
							<a href="http://www.codeorigin.cn/category/nodejs">nodejs</a>
						</li>
						<li id="menu-item-27" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-27">
							<a href="http://www.codeorigin.cn/category/linux">linux</a>
						</li>
						<li id="menu-item-31" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-31">
							<a href="http://www.codeorigin.cn/category/xiantan">闲谈</a>
						</li>
						<li id="menu-item-157" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-157">
							<a href="http://www.codeorigin.cn/aboutme">关于我</a>
						</li>
					</ul>
				</nav>
			</div>
		</header>

		<header class="mobile">
			<nav class="navbar navbar-inverse" role="navigation">
				<div class="container-fluid">
					<div class="navbar-header">
						<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#mobile-nav">
					<span class="sr-only">切换导航</span>
					<span class="icon-bar"></span>
					<span class="icon-bar"></span>
					<span class="icon-bar"></span>
				</button>
						<a class="navbar-brand" href="#">CodeOrigin</a>
					</div>
					<div id="mobile-nav" class="collapse navbar-collapse">
						<ul id="menu-%e5%89%8d%e7%ab%af%e6%8a%80%e6%9c%af-1" class="nav navbar-nav">
							<li class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor current-menu-parent current-post-parent menu-item-has-children menu-item-23">
								<a href="http://www.codeorigin.cn/category/qdjs">前端技术</a>
								<ul class="sub-menu">
									<li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-24">
										<a href="http://www.codeorigin.cn/category/qdjs/javascript">javascript</a>
									</li>
									<li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-29">
										<a href="http://www.codeorigin.cn/category/qdjs/html5">html5</a>
									</li>
									<li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-33">
										<a href="http://www.codeorigin.cn/category/qdjs/css3">css3</a>
									</li>
								</ul>
							</li>
							<li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-25">
								<a href="http://www.codeorigin.cn/category/mongodb">MongoDb</a>
							</li>
							<li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-28">
								<a href="http://www.codeorigin.cn/category/nodejs">nodejs</a>
							</li>
							<li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-27">
								<a href="http://www.codeorigin.cn/category/linux">linux</a>
							</li>
							<li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-31">
								<a href="http://www.codeorigin.cn/category/xiantan">闲谈</a>
							</li>
							<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-157">
								<a href="http://www.codeorigin.cn/aboutme">关于我</a>
							</li>
						</ul>
					</div>
					<!--<div class="collapse navbar-collapse" id="example-navbar-collapse">
				<ul class="nav navbar-nav">
					<li class="active"><a href="#">iOS</a></li>
					<li><a href="#">SVN</a></li>
				</ul>
				<form class="navbar-form navbar-left" role="search">
		            <div class="form-group">
		                <input type="text" class="form-control" placeholder="Search">
		            </div>
		            <button type="submit" class="btn btn-default">搜索</button>
		        </form>
			</div>-->
				</div>
			</nav>
		</header>
		<!------------------------------------------ 头部代码end ---------------------------------->

		<section class="w1180 index cl cd-main-content">
			<!--========================index 左侧  开始================================-->
			<div class="deanpol left cl">
				<div class="deanpol aiv_2 cl">
					<nav class="crumbs">现在位置：
						<a title="返回首页" href="http://www.codeorigin.cn">首页</a> &gt;
						<a href="http://www.codeorigin.cn/category/qdjs" rel="category tag">前端技术</a> &gt; 正文 </nav>
				</div>
				<!--首页end-->

				<!--首页列表-->
				<div class="deanpol aiv cl">
					<div class="ae_ct">

						<article id="post-126" class="post-126 post type-post status-publish format-standard hentry category-qdjs tag-28 tag-25">
							<header class="single-header">
								<h1>如何书写正则表达式(RegPxp)</h1>
								<div class="single-meta">
									<div class="time"><i class="fa fa-clock-o"></i>
										<a href="javascript:void(0);">2016 /10/18 11:10</a>
									</div>
									<!--0-->
									<div class="comments xs-hidden"><i class="fa fa-comments fa-1"></i>
										<a href="http://www.codeorigin.cn/qdjs/126.html#respond">沙发</a>
									</div>

								</div>
								<!-- .entry-meta -->
							</header>
							<!-- .single-header -->
							<!--目录开始-->
							<div class="lemmaWgt-lemmaCatalog">
								<div class="block-title">目录</div>
								<div class="catalog-list" id="catalog-list">
									<ol>
										<li><a href="#test">创建方法</a>
											<ul>
												<li>直接量语法</li>
												<li>对象构造法</li>
												<li>参数说明</li>
												<li>实例</li>
												<li>读一个正则表达式</li>
											</ul>
										</li>
										<li>实例属性</li>
										<li>实例方法
											<ul>
												<li>exec(str)</li>
												<li>test(str)</li>
												<li>问题</li>
											</ul>
										</li>
										<li>附录
											<ul>
												<li>方括号</li>
												<li>元字符</li>
												<li>量词</li>
											</ul>
										</li>
										<li>参考</li>
									</ol>
								</div>
								<div style="clear: both;"></div>
							</div>
							<!--目录结束-->
							<div class="single-main">
								<p>正则表达式被广泛用于各语言中的字符串匹配。如数据格式校验、字符串查找等，下面以JavaScript为例，讲讲关于它的写法，当然其它语言也大同小异。这里搜集了一些
									<a href="http://www.codeorigin.cn/qdjs/171.html" target="_blank">常用的正则表达式</a>，可用于日常的项目中。</p>
								<h2>创建方法</h2>
								<h3>直接量语法</h3>
								<p>/pattern/flags</p>
								<h3>对象构造法</h3>
								<p>new RegExp(pattern, flags)</p>
								<h3>参数说明</h3>
								<p><span style="line-height: 1.5;">pattern（模式）：可以是任何简单或复杂的正则表达式，可以是字符类、限定符、分组、向前查找及方向引用。对象构造法中使用</span><strong>正则表达式字符串</strong><span style="line-height: 1.5;">，如果 </span><i>pattern</i><span style="line-height: 1.5;"> 是正则表达式，而不是字符串，则必须省略flags参数。模式中使用</span><strong>元字符（</strong><span style="line-height: 1.5;">(  )  [  ]  \  ^  $  |  ?  *  +  .  {  }</span><strong>）</strong><span style="line-height: 1.5;">时都必须转义，因此字符串中需要双重转义。</span></p>
								<table style="width: 60%; margin: 0px auto;" border="1" cellspacing="0" cellpadding="2">
									<tbody>
										<tr>
											<td style="text-align: center;">字面量模式</td>
											<td style="text-align: center;">等价的字符串</td>
											<td style="text-align: center;">描述</td>
										</tr>
										<tr>
											<td style="text-align: center;">/\.at/</td>
											<td style="text-align: center;">&#8220;\\.at&#8221;</td>
											<td style="text-align: center;">匹配“.at”字符串</td>
										</tr>
										<tr>
											<td style="text-align: center;">/\\hello/</td>
											<td style="text-align: center;">&#8220;\\\\hello&#8221;</td>
											<td style="text-align: center;">匹配“\hello”字符串</td>
										</tr>
									</tbody>
								</table>
								<p><span style="line-height: 1.5;">flags（标识）：用于表明正则表达式的行为。</span></p>
								<table style="width: 60%; margin: 0px auto;" border="1" cellspacing="0" cellpadding="2">
									<tbody>
										<tr>
											<td style="text-align: center;">标识</td>
											<td style="text-align: center;">描述</td>
										</tr>
										<tr>
											<td style="text-align: center;">i</td>
											<td style="text-align: center;">执行对大小写不敏感的匹配。</td>
										</tr>
										<tr>
											<td style="text-align: center;">g</td>
											<td style="text-align: center;">执行全局匹配（查找所有匹配而非在找到第一个匹配后停止）。</td>
										</tr>
										<tr>
											<td style="text-align: center;">m</td>
											<td style="text-align: center;">执行多行匹配。</td>
										</tr>
									</tbody>
								</table>
								<h3>实例</h3>
								<p>一个正则表达式是<span style="color: #ff0000;">一个模式和3个标识的组合体</span>。</p>

								<div class="wp_syntax">
									<table>
										<tr>
											<td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td>
											<td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//区分大小写匹配所有“at”的组合</span>
<span style="color: #000066; font-weight: bold;">var</span> p1 <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/at/g</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//var p1 = new RegExp(/at/g);</span>
<span style="color: #006600; font-style: italic;">//var p1 = new RegExp('at', 'g');</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//不区分大小写匹配所有以at结尾的3个字符的组合</span>
<span style="color: #000066; font-weight: bold;">var</span> p2 <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/.at/gi</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//var p2 = new RegExp(/.at/gi);</span>
<span style="color: #006600; font-style: italic;">//var p2 = new RegExp('.at', 'gi');</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//不区分大小写匹配所有以“.at”结尾的3个字符的组合</span>
<span style="color: #000066; font-weight: bold;">var</span> p3 <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/\.at/gi</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//var p3 = new RegExp(/\.at/gi);</span>
<span style="color: #006600; font-style: italic;">//var p3 = new RegExp('\\.at', 'gi');</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//不区分大小写匹配第一个“\hello”字符串</span>
<span style="color: #000066; font-weight: bold;">var</span> p4 <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/\\hello/</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//var p4 = new RegExp(/\\hello/);</span>
<span style="color: #006600; font-style: italic;">//var p4 = new RegExp('\\\\hello');</span></pre></td>
										</tr>
									</table>
								</div>

								<h3>读一个正则表达式</h3>
								<p>var p1 = new RegExp(&#8216;<span style="color: #e53333;">^1</span><span style="color: #337fe5;">[0-9]{10}$</span>&#8216;, &#8216;i&#8217;);</p>
								<p><span style="color: #e53333;">以1开始</span>，后面接<span style="color: #337fe5;">由10个0到9字符组成的字符串结尾的<span style="color: #000000;">11位长度的</span></span>数据。</p>

								<div class="wp_syntax">
									<table>
										<tr>
											<td class="line_numbers"><pre>1
2
3
4
</pre></td>
											<td class="code"><pre class="javascript" style="font-family:monospace;">p1.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'15088135140x'</span><span style="color: #009900;">&#41;</span>   <span style="color: #006600; font-style: italic;">//false    不是以数字结尾</span>
p1.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'25088135140'</span><span style="color: #009900;">&#41;</span>   <span style="color: #006600; font-style: italic;">//flase    不是以1开始</span>
p1.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'250881351403'</span><span style="color: #009900;">&#41;</span>   <span style="color: #006600; font-style: italic;">//flase    不是11</span>
p1.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'15088135140'</span><span style="color: #009900;">&#41;</span>   <span style="color: #006600; font-style: italic;">//true    符合规则</span></pre></td>
										</tr>
									</table>
								</div>

								<h2>实例属性</h2>
								<p><span style="line-height: 1.5;">global：布尔值，表示是否设置g标识</span><br />
									<span style="line-height: 1.5;">ignoreCase：布尔值，表示是否设置了i标识</span><br />
									<span style="line-height: 1.5;">lastIndex：整数，表示开始搜索下一个匹配项的字符位置，从0开始</span><br />
									<span style="line-height: 1.5;">multiline：布尔值，表示是否设置了m标识</span><br />
									<span style="line-height: 1.5;">source：正则表达式的字符串表示，按照字面量形式而非传入构造函数中的字符串模式返回（可参照下面代码）。</span></p>

								<div class="wp_syntax">
									<table>
										<tr>
											<td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td>
											<td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//var p1 = /\\hello/i;</span>
<span style="color: #000066; font-weight: bold;">var</span> p1 <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\\</span>hello'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'i'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
alert<span style="color: #009900;">&#40;</span>p1.<span style="color: #660066;">global</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     <span style="color: #006600; font-style: italic;">//false</span>
alert<span style="color: #009900;">&#40;</span>p1.<span style="color: #660066;">ignoreCase</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #006600; font-style: italic;">//true</span>
alert<span style="color: #009900;">&#40;</span>p1.<span style="color: #660066;">lastIndex</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>      <span style="color: #006600; font-style: italic;">//0</span>
alert<span style="color: #009900;">&#40;</span>p1.<span style="color: #660066;">multiline</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>       <span style="color: #006600; font-style: italic;">//false</span>
alert<span style="color: #009900;">&#40;</span>p1.<span style="color: #660066;">source</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>         <span style="color: #006600; font-style: italic;">//&quot;\\hello&quot;</span></pre></td>
										</tr>
									</table>
								</div>

								<p>source属性保存的是规范形式的字符串，即字面量形式所用的字符串</p>
								<h2>实例方法</h2>
								<h3>exec(str)</h3>
								<p>接受参数：应用模式的字符串</p>
								<p>返回值：包含第一个匹配项的数组，该数组包含两个额外的属性：index和input，index表示匹配项的字符串，input表示应用正则表达式的字符串，没有匹配项时返回null</p>

								<div class="wp_syntax">
									<table>
										<tr>
											<td class="line_numbers"><pre>1
2
3
4
</pre></td>
											<td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> <span style="color: #3366CC;">'cat,bat,sat,fat'</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> p2 <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/.at/</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> matchs <span style="color: #339933;">=</span> p2.<span style="color: #660066;">exec</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>matchs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        <span style="color: #006600; font-style: italic;">//[&quot;cat&quot;, index: 0, input: &quot;cat,bat,sat,fat&quot;]</span></pre></td>
										</tr>
									</table>
								</div>

								<h3>test(str)</h3>
								<p>接受参数：应用模式的字符串</p>
								<p>返回值：布尔值，模式与参数匹配是返回ture，不匹配是返回false</p>

								<div class="wp_syntax">
									<table>
										<tr>
											<td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td>
											<td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> text1 <span style="color: #339933;">=</span> <span style="color: #3366CC;">'cat,bat,sat,fat'</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> text2 <span style="color: #339933;">=</span> <span style="color: #3366CC;">'hello'</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> p2 <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/.at/</span><span style="color: #339933;">;</span>
&nbsp;
console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span>p2.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>text1<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>  <span style="color: #006600; font-style: italic;">//true</span>
console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span>p2.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>text2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>  <span style="color: #006600; font-style: italic;">//false</span></pre></td>
										</tr>
									</table>
								</div>

								<h3>问题</h3>

								<div class="wp_syntax">
									<table>
										<tr>
											<td class="line_numbers"><pre>1
2
3
4
5
</pre></td>
											<td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> <span style="color: #3366CC;">'hello'</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> p2 <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/hello/g</span><span style="color: #339933;">;</span>
&nbsp;
console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span>p2.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>   <span style="color: #006600; font-style: italic;">//true</span>
console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span>p2.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>   <span style="color: #006600; font-style: italic;">//false</span></pre></td>
										</tr>
									</table>
								</div>

								<div class="wp_syntax">
									<table>
										<tr>
											<td class="line_numbers"><pre>1
2
3
4
5
</pre></td>
											<td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> <span style="color: #3366CC;">'hello'</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> p2 <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/hello/</span><span style="color: #339933;">;</span>
&nbsp;
console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span>p2.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>   <span style="color: #006600; font-style: italic;">//true</span>
console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span>p2.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>   <span style="color: #006600; font-style: italic;">//true</span></pre></td>
										</tr>
									</table>
								</div>

								<p>上面出现了一个奇怪的问题：正则表达式p2在g标识匹配时，第一次结果正确，可是当连续调用test方法却得到了不同的返回结果。</p>
								<p>其实对于这个问题换成exec()也许会更明显一点</p>

								<div class="wp_syntax">
									<table>
										<tr>
											<td class="line_numbers"><pre>1
2
3
4
5
</pre></td>
											<td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> <span style="color: #3366CC;">'hello'</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> p2 <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/hello/g</span><span style="color: #339933;">;</span>
&nbsp;
console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span>p2.<span style="color: #660066;">exec</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>   <span style="color: #006600; font-style: italic;">//[&quot;hello&quot;, index: 0, input: &quot;hello&quot;]</span>
console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span>p2.<span style="color: #660066;">exec</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>   <span style="color: #006600; font-style: italic;">//null</span></pre></td>
										</tr>
									</table>
								</div>

								<p>上面代码可以看出第一次调用返回了匹配数组，而第二次返回了null说明没有匹配到。</p>

								<div class="wp_syntax">
									<table>
										<tr>
											<td class="line_numbers"><pre>1
2
3
4
5
</pre></td>
											<td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> <span style="color: #3366CC;">'cat,bat,sat,fat'</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> p2 <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/.at/g</span><span style="color: #339933;">;</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>p2.<span style="color: #660066;">exec</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        <span style="color: #006600; font-style: italic;">//[&quot;cat&quot;, index: 0, input: &quot;cat,bat,sat,fat&quot;]</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>p2.<span style="color: #660066;">exec</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        <span style="color: #006600; font-style: italic;">//[&quot;bat&quot;, index: 4, input: &quot;cat,bat,sat,fat&quot;]</span></pre></td>
										</tr>
									</table>
								</div>

								<p>从上面的代码其实可以看到，在g全局模式下，两次返回的匹配项数组中的index是有区别的，也就是在该模式下，第二次匹配执行会从第一次匹配的结尾处开始，直到整个字符串结束才结束，关于g模式下匹配“hello”时，第一次命中了，所以返回正确的结果，可第二次执行时，从第一次的命中项目“hello”结尾处开始到整个字符串结束(中间还有一个结束符)，所以第二次返回了false；而在非全局模式下不会出现这种问题，因为它每次都是从第一个开始，命中了就返回。</p>

								<div class="wp_syntax">
									<table>
										<tr>
											<td class="line_numbers"><pre>1
2
3
4
5
</pre></td>
											<td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> <span style="color: #3366CC;">'cat,bat,sat,fat'</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> p2 <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/.at/</span><span style="color: #339933;">;</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>p2.<span style="color: #660066;">exec</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        <span style="color: #006600; font-style: italic;">//[&quot;cat&quot;, index: 0, input: &quot;cat,bat,sat,fat&quot;]</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>p2.<span style="color: #660066;">exec</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        <span style="color: #006600; font-style: italic;">//[&quot;cat&quot;, index: 0, input: &quot;cat,bat,sat,fat&quot;] </span></pre></td>
										</tr>
									</table>
								</div>

								<h2>附录</h2>
								<h3>方括号</h3>
								<p>方括号用于查找某个范围内的字符：</p>
								<table class="dataintable">
									<tbody>
										<tr>
											<th>表达式</th>
											<th>描述</th>
										</tr>
										<tr>
											<td>[abc]</td>
											<td>查找方括号之间的任何字符。</td>
										</tr>
										<tr>
											<td>[^abc]</td>
											<td>查找任何不在方括号之间的字符。</td>
										</tr>
										<tr>
											<td>[0-9]</td>
											<td>查找任何从 0 至 9 的数字。</td>
										</tr>
										<tr>
											<td>[a-z]</td>
											<td>查找任何从小写 a 到小写 z 的字符。</td>
										</tr>
										<tr>
											<td>[A-Z]</td>
											<td>查找任何从大写 A 到大写 Z 的字符。</td>
										</tr>
										<tr>
											<td>[A-z]</td>
											<td>查找任何从大写 A 到小写 z 的字符。</td>
										</tr>
										<tr>
											<td>[adgk]</td>
											<td>查找给定集合内的任何字符。</td>
										</tr>
										<tr>
											<td>[^adgk]</td>
											<td>查找给定集合外的任何字符。</td>
										</tr>
										<tr>
											<td>(red|blue|green)</td>
											<td>查找任何指定的选项。</td>
										</tr>
									</tbody>
								</table>
								<h3>元字符</h3>
								<p>元字符（Metacharacter）是拥有特殊含义的字符：</p>
								<table class="dataintable">
									<tbody>
										<tr>
											<th>元字符</th>
											<th>描述</th>
										</tr>
										<tr>
											<td>.</td>
											<td>查找单个字符，除了换行和行结束符。</td>
										</tr>
										<tr>
											<td>\w</td>
											<td>查找单词字符。</td>
										</tr>
										<tr>
											<td>\W</td>
											<td>查找非单词字符。</td>
										</tr>
										<tr>
											<td>\d</td>
											<td>查找数字。</td>
										</tr>
										<tr>
											<td>\D</td>
											<td>查找非数字字符。</td>
										</tr>
										<tr>
											<td>\s</td>
											<td>查找空白字符。</td>
										</tr>
										<tr>
											<td>\S</td>
											<td>查找非空白字符。</td>
										</tr>
										<tr>
											<td>\b</td>
											<td>匹配单词边界。</td>
										</tr>
										<tr>
											<td>\B</td>
											<td>匹配非单词边界。</td>
										</tr>
										<tr>
											<td>\0</td>
											<td>查找 NUL 字符。</td>
										</tr>
										<tr>
											<td>\n</td>
											<td>查找换行符。</td>
										</tr>
										<tr>
											<td>\f</td>
											<td>查找换页符。</td>
										</tr>
										<tr>
											<td>\r</td>
											<td>查找回车符。</td>
										</tr>
										<tr>
											<td>\t</td>
											<td>查找制表符。</td>
										</tr>
										<tr>
											<td>\v</td>
											<td>查找垂直制表符。</td>
										</tr>
										<tr>
											<td>\xxx</td>
											<td>查找以八进制数 xxx 规定的字符。</td>
										</tr>
										<tr>
											<td>\xdd</td>
											<td>查找以十六进制数 dd 规定的字符。</td>
										</tr>
										<tr>
											<td>\uxxxx</td>
											<td>查找以十六进制数 xxxx 规定的 Unicode 字符。</td>
										</tr>
									</tbody>
								</table>
								<h3>量词</h3>
								<table class="dataintable">
									<tbody>
										<tr>
											<th>量词</th>
											<th>描述</th>
										</tr>
										<tr>
											<td>n+</td>
											<td>匹配任何包含至少一个 n 的字符串。</td>
										</tr>
										<tr>
											<td>n*</td>
											<td>匹配任何包含零个或多个 n 的字符串。</td>
										</tr>
										<tr>
											<td>n?</td>
											<td>匹配任何包含零个或一个 n 的字符串。</td>
										</tr>
										<tr>
											<td>n{X}</td>
											<td>匹配包含 X 个 n 的序列的字符串。</td>
										</tr>
										<tr>
											<td>n{X,Y}</td>
											<td>匹配包含 X 或 Y 个 n 的序列的字符串。</td>
										</tr>
										<tr>
											<td>n{X,}</td>
											<td>匹配包含至少 X 个 n 的序列的字符串。</td>
										</tr>
										<tr>
											<td>n$</td>
											<td>匹配任何结尾为 n 的字符串。</td>
										</tr>
										<tr>
											<td>^n</td>
											<td>匹配任何开头为 n 的字符串。</td>
										</tr>
										<tr>
											<td>?=n</td>
											<td>匹配任何其后紧接指定字符串 n 的字符串。</td>
										</tr>
										<tr>
											<td>?!n</td>
											<td>匹配任何其后没有紧接指定字符串 n 的字符串。</td>
										</tr>
									</tbody>
								</table>
								<h2 id="test">参考</h2>
								<ul>
									<li>w3school 在线教程 http://www.w3school.com.cn/</li>
									<li>《JavaScript》高级程序设计（第三版）</li>
								</ul>
								<div class="bq_cx">来源：本文由
									<a href="http://www.codeorigin.cn">分享IT技术|码哥博客</a>原创撰写，欢迎分享本文，转载请保留出处和链接！</div>
							</div>
							<!-- .single-content -->
						</article>
					</div>
					<div id="related_post-2" class="widget-container widget_related_post hidden-xs">
						<h3 class="widget-title">相关文章</h3>
						<ul class="related_post">
							<li>
								<a href="http://www.codeorigin.cn/qdjs/171.html" title="常用正则表达式大全"><img src="http://www.codeorigin.cn/wp-content/themes/wp-dfrobot-po/timthumb.php?src=http://www.codeorigin.cn/wp-content/themes/wp-dfrobot-po/images/demo/1.jpg&w=273&h=180&zc=1" width="172" height="112" alt="常用正则表达式大全">
									<p>常用正则表达式大全</p>
								</a>
							</li>
							<li>
								<a href="http://www.codeorigin.cn/qdjs/104.html" title="JavaScript混淆加密解密压缩优点|方法|报错解决"><img src="http://www.codeorigin.cn/wp-content/themes/wp-dfrobot-po/timthumb.php?src=http://www.codeorigin.cn/wp-content/myuploads/2016/10/5-1024x120.png&w=273&h=180&zc=1" width="172" height="112" alt="JavaScript混淆加密解密压缩优点|方法|报错解决">
									<p>JavaScript混淆加密解密压缩优点|方法|报错解决</p>
								</a>
							</li>
							<li>
								<a href="http://www.codeorigin.cn/qdjs/44.html" title="如何制作jq插件-回到顶部jq插件制作"><img src="http://www.codeorigin.cn/wp-content/themes/wp-dfrobot-po/timthumb.php?src=http://www.codeorigin.cn/wp-content/myuploads/2016/10/0161010135328-e1476079789193.png&w=273&h=180&zc=1" width="172" height="112" alt="如何制作jq插件-回到顶部jq插件制作">
									<p>如何制作jq插件-回到顶部jq插件制作</p>
								</a>
							</li>
						</ul>
					</div>

					<!-- 引用 -->
					<div id="comments" class="comments-area">
						<h2 class="comments-title">
		抢沙发
	</h2>
						<div id="respond" class="comment-respond row">

							<form action="http://www.codeorigin.cn/wp-comments-post.php" method="post" id="commentform">

								<div class="welcomediv">
									<img src="https://secure.gravatar.com/avatar/f13e35245497c798875d4aeb90c8b587?s=30" class="avatar" height="30" width="30"> 登录者：
									<a href="http://www.codeorigin.cn">mark</a>
									<a href="http://www.codeorigin.cn/wp-login.php?action=logout&amp;redirect_to=http%3A%2F%2Fwww.codeorigin.cn%2Fqdjs%2F126.html&amp;_wpnonce=4f96b4ecf5" title="退出"> 退出</a>
								</div>

								<div class=" cominfodiv-nr">
									<textarea class="texty" name="comment" id="comment" rows="10" tabindex="4" placeholder="输入评论内容..."></textarea>
									<div class="submitcom">
										<button id="submit" name="submit" type="button" class="btn btn-warning popover-hide mobile" title="" data-container="body" data-toggle="popover" data-placement="left">
							提交留言
						</button>
										<input class="screen" id="submit" name="submit" type="submit" tabindex="5" value="提&nbsp;交" />
										<input type='hidden' name='comment_post_ID' value='126' id='comment_post_ID' />
										<input type='hidden' name='comment_parent' id='comment_parent' value='0' />
										<input type="hidden" id="_wp_unfiltered_html_comment_disabled" name="_wp_unfiltered_html_comment_disabled" value="f2aa522038" />
										<script>
											(function() {
												if(window === window.parent) {
													document.getElementById('_wp_unfiltered_html_comment_disabled').name = '_wp_unfiltered_html_comment';
												}
											})();
										</script>
									</div>
									<div id="cancel_comment_reply">
										<a rel="nofollow" id="cancel-comment-reply-link" href="/qdjs/126.html#respond" style="display:none;">取消回复</a>
									</div>
								</div>
							</form>
						</div>

					</div>
					<!-- #comments -->
				</div>
				<!--首页end-->
			</div>
			<!--=============================侧边开始===================================-->
			<div class="sidebar right cl">
				<!--login-->
				<div class="deanpor cl">
					<img class="zuozeipc" src="http://www.codeorigin.cn/wp-content/myuploads/2016/10/post-lz.png">
					<div class="deanunlogin_index">
						<a href="#">
							<img src="http://www.codeorigin.cn/wp-content/myuploads/2016/10/53245faf8a2ed399.jpg200x200.jpg" class="avatar" width="100" height="100">
						</a>
					</div>
					<div class="um_huname">
						<a href="/author/1/" title="码哥" rel="author">码哥</a>
						<a class="wzzs" href="/author/1/?tab=index" title="作者文章">（共5篇文章）</a>
					</div>
					<div class="authordes">
						<i class="fa fa-quote-left"></i> 码哥专注于WEB前端IT技术开发，包括但不限于Jquery、CSS、javascript、Linux、nodejs、mongoDb等。小站旨在交流及分享技术心得，同时贡献WEB相关的原创技术文章。
						<i class="fa fa-quote-right"></i>
					</div>
					<div class="zx_new">
						<span><i class="fa fa-paper-plane"></i>最新文章：</span>
						<a href="http://www.codeorigin.cn/qdjs/171.html"> 常用正则表达式大全 </a>
					</div>
				</div>
				<!--login end-->
				<div class="sidebar_tow cl">
					<div class="summary2">
						<a href="http://www.codeorigin.cn/qdjs/126.html" rel="bookmark" title="如何书写正则表达式(RegPxp)">手气不错</a>
					</div>
					<h2 class="deanrsidet"><span>热门话题</span></h2>
					<ul class="s_bian cl">
						<li class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor current-menu-parent current-post-parent menu-item-has-children menu-item-23">
							<a href="http://www.codeorigin.cn/category/qdjs">前端技术</a>
							<ul class="sub-menu">
								<li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-24">
									<a href="http://www.codeorigin.cn/category/qdjs/javascript">javascript</a>
								</li>
								<li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-29">
									<a href="http://www.codeorigin.cn/category/qdjs/html5">html5</a>
								</li>
								<li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-33">
									<a href="http://www.codeorigin.cn/category/qdjs/css3">css3</a>
								</li>
							</ul>
						</li>
						<li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-25">
							<a href="http://www.codeorigin.cn/category/mongodb">MongoDb</a>
						</li>
						<li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-28">
							<a href="http://www.codeorigin.cn/category/nodejs">nodejs</a>
						</li>
						<li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-27">
							<a href="http://www.codeorigin.cn/category/linux">linux</a>
						</li>
						<li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-31">
							<a href="http://www.codeorigin.cn/category/xiantan">闲谈</a>
						</li>
						<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-157">
							<a href="http://www.codeorigin.cn/aboutme">关于我</a>
						</li>
					</ul>
					<h2 class="deanrsidet"><span>手机扫码</span></h2>
					<div class="erwei cl"><img src="http://www.codeorigin.cn/wp-content/myuploads/2016/10/2erma.png" width="338" height="187" alt="网站二维码扫描" /></div>
					<h2 class="deanrsidet"><span>精选内容</span></h2>
					<ul class="deanjctj cl">
						<li>
							<a href="http://www.codeorigin.cn/qdjs/126.html" title="如何书写正则表达式(RegPxp)">如何书写正则表达式(RegPxp)</a>
						</li>
						<li>
							<a href="http://www.codeorigin.cn/qdjs/104.html" title="JavaScript混淆加密解密压缩优点|方法|报错解决">JavaScript混淆加密解密压缩优点|方法|报错解决</a>
						</li>
						<li>
							<a href="http://www.codeorigin.cn/qdjs/44.html" title="如何制作jq插件-回到顶部jq插件制作">如何制作jq插件-回到顶部jq插件制作</a>
						</li>
					</ul>
				</div>
			</div>

			<!--=============================右侧end===================================-->
		</section>
		<!---------------------------- footer -------------------------------------->
		<footer>
			<div class="w1180 cl">
				<hgroup class="flogo left"></hgroup>
				<div class="deanftbm">
					<p>
						<a href="http://www.codeorigin.cn/category/qdjs">前端技术</a>

						<a href="http://www.codeorigin.cn/category/qdjs/javascript">javascript</a>
						<a href="http://www.codeorigin.cn/category/qdjs/html5">html5</a>
						<a href="http://www.codeorigin.cn/category/qdjs/css3">css3</a>

						<a href="http://www.codeorigin.cn/category/mongodb">MongoDb</a>
						<a href="http://www.codeorigin.cn/category/nodejs">nodejs</a>
						<a href="http://www.codeorigin.cn/category/linux">linux</a>
						<a href="http://www.codeorigin.cn/category/xiantan">闲谈</a>
						<a href="http://www.codeorigin.cn/aboutme">关于我</a>
						<span></span></p>
					<p>码哥博客专注于为互联网贡献WEB相关的技术文章，包括但不限于WEB前端、Jquery、CSS、js、Linux等，转载请保留出处和链接！</p>
				</div>
				<div class="deanftbr">
					<p>主题作者：</p>
					<p>晨星博客 @小牛爱奋斗</p>
				</div>
			</div>
		</footer>

		<div id="elevator_item">
			<a id="elevator" onclick="return false;" title="回到顶部"><i class="fa fa-angle-up"></i></a>
		</div>
	</body>

</html>